package com.dreamana.components
{
	import com.dreamana.ui.BaseComponent;
	import com.dreamana.ui.SkinGraphics;
	
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.geom.Rectangle;
	
	public class ProgressBar extends BaseComponent
	{
		protected var _track:Shape;
		protected var _trackColor:int = -1;
		protected var _trackTexture:BitmapData;
		protected var _track9Grid:Rectangle;
		
		protected var _face:Shape;
		protected var _faceColor:int = -1;
		protected var _faceTexture:BitmapData;
		protected var _face9Grid:Rectangle;
		
		protected var _percent:Number = 0.0;
		
		
		public function ProgressBar(w:int, h:int)
		{
			_width = w;
			_height = h;
			
			this.init();
		}
		
		override protected function addChildren():void
		{
			_track = new Shape();
			this.addChild(_track);
			
			_face = new Shape();
			this.addChild(_face);
		}
		
		//--- PUBLIC METHODS ---
		
		override public function draw():void
		{
			var g:Graphics;
			
			g = _track.graphics;
			g.clear();
			if(_trackTexture) {
				if(_track9Grid) SkinGraphics.fill9Grid(g, _trackTexture, _width, _height, _track9Grid, false);
				else SkinGraphics.fillRect(g, _trackTexture, _width, _height);
			}
			else if(_trackColor >= 0) {
				SkinGraphics.drawBlank(g, _width, _height, _trackColor);
			}
			else {
				SkinGraphics.drawWireFrame(g, _width, _height);
			}
			
			var barWidth:int = int(_width * _percent + 0.5);
			
			g = _face.graphics;
			g.clear();
			if(barWidth > 0) {
				if(_faceTexture) {
					if(_face9Grid) SkinGraphics.fill9Grid(g, _faceTexture, barWidth, _height, _face9Grid, false);
					else SkinGraphics.fillRect(g, _faceTexture, barWidth, _height);
				}
				else if(_faceColor >= 0) {
					SkinGraphics.drawBlank(g, barWidth, _height, _faceColor);
				}
				else {
					SkinGraphics.drawWireFrame(g, barWidth, _height);
				}
			}
		}
		
		override public function setStyle(style:String, value:Object):void
		{
			super.setStyle(style, value);
			
			switch(style) {
				case "track-color":
					_trackColor = value as Number;
					invalidate();
					break;
				case "track-image":
					_trackTexture = value as BitmapData;
					invalidate();
					break;
				case "track-9grid":
					_track9Grid = value as Rectangle;
					invalidate();
					break;
				case "face-color":
					_faceColor = value as Number;
					invalidate();
					break;
				case "face-image":
					_faceTexture = value as BitmapData;
					invalidate();
					break;
				case "face-9grid":
					_face9Grid = value as Rectangle;
					invalidate();
					break;
			}
		}
		
		//--- EVENT HANDLERS ---
		
		
		//--- GETTER/SETTERS ---		
		public function get percent():Number { return _percent; }
		public function set percent(value:Number):void {
			_percent = value;
			invalidate();
		}
		
		public function get track():Shape { return _track; }
		public function get face():Shape { return _face; }
	}
}